package com.dh.weibo;

import com.dh.weibo.application.Application;
import com.dh.weibo.controller.UserController;
import com.dh.weibo.controller.WeiBoController;
import com.dh.weibo.exception.AccountReadyException;
import com.dh.weibo.pojo.Comment;
import com.dh.weibo.pojo.User;
import com.dh.weibo.pojo.WeiBo;
import com.dh.weibo.pojo.vo.WeiBoVo;
import com.dh.weibo.util.DateTimeUtil;

import java.util.*;

public class WeiBoMain {
    private static Scanner scanner = new Scanner(System.in);
    private static UserController userController = new UserController();
    private static WeiBoController weiBoController = new WeiBoController();

    public static void main(String[] args) {
        System.out.println("欢迎来到微博管理系统");
        while (true) {
            printLoginMenu();
            int num = scanner.nextInt();
            if (num == 1) {
                register();
            } else if (num == 2) {
                boolean result = login();
                if (result) {
                    if (Application.user.isSuper()) {
                        while (true) {
                            printAdminMenu();
                            int num1 = scanner.nextInt();
                            if (num1 == 1) {
                                getWeiBos();
                            } else if (num1 == 2) {
                                deleteWeiBo();
                            } else if (num1 == 3) {
                                addUser();
                            } else if (num1 == 4) {
                                blackUser();
                            } else if (num1 == 5) {
                                deleteComment();
                            } else if (num1 == 6) {
                                break;
                            }
                        }
                    } else {
                        while (true) {
                            printMenu();
                            int num2 = scanner.nextInt();
                            if (num2 == 1) {
                                publish();
                            } else if (num2 == 2) {
                                getWeiBos();
                            } else if (num2 == 3) {
                                comment();
                            } else if (num2 == 4) {
                                getComment();
                            } else if (num2 == 5) {

                            } else if (num2 == 6) {

                            } else if (num2 == 7) {

                            } else if (num2 == 8) {
                                break;
                            }
                        }
                    }
                } else {
                    System.out.println("用户账号或密码错误，请联系管理员");
                }
            }

        }
    }

    private static void deleteComment() {
        List<Comment> comments = weiBoController.getComments();
        for (Comment comment : comments) {
            System.out.println(comment);
        }
        System.out.println("请输入要删除评论ID");
        String cid = scanner.next();
        weiBoController.deleteComment(cid);
    }

    /**
     * 添加用户
     */
    public static void addUser() {
        register();
    }

    // 冻结用户
    private static void blackUser() {
        List<User> users = userController.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
        System.out.println("请输入要冻结的用户ID");
        String id = scanner.next();
        userController.freeze(id);
    }

    private static void deleteWeiBo() {
        System.out.println("请输入要删除微博ID");
        String id = scanner.next();
        weiBoController.delete(id);
    }


    public static void printAdminMenu() {
        System.out.println("1.广场微博");
        System.out.println("2.删除微博");
        System.out.println("3.添加用户");
        System.out.println("4.冻结用户");
        System.out.println("5.删除评论");
        System.out.println("6.退出");
        System.out.println("7.按照评论数排序");
        System.out.println("8.查看评论数最高的微博");
    }

    public static void printMenu() {
        System.out.println("1.发布微博");
        System.out.println("2.广场微博");
        System.out.println("3.评论微博");
        System.out.println("4.查看评论");
        System.out.println("5.点赞微博");
        System.out.println("6.收藏微博");
        System.out.println("7.查看评论过的微博");// 显示的微博数据
        System.out.println("8.退出");
    }

    public static void printLoginMenu() {
        System.out.println("1.注册");
        System.out.println("2.登陆");
    }

    public static void register() {
        System.out.println("请输入账号:");
        String account = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();

        System.out.println("请输入昵称：");
        String nickName = scanner.next();
        try {
            userController.register(account, password, nickName);
        } catch (AccountReadyException e) {
            System.out.println("账号已存在");
        }
    }

    public static boolean login() {
        System.out.println("请输入账号");
        String account = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        User user = userController.login(account, password);
        if (user != null && user.getStatus() == 0) {
            Application.user = user;// 持久化user对象
            return true;
        }
        return false;
    }

    public static void publish() {

        weiBoController.publish("今天天气真好" + DateTimeUtil.getCurrentTimeMillis(), "38.2");
    }

    public static void getWeiBos() {
//        List<WeiBo> weiBos = weiBoController.getWeiBos();
//        for (WeiBo weiBo : weiBos) {
//            System.out.println(weiBo);
//        }

//           -----------------------------------------------------------------------------------
//        HashMap<WeiBo, List<Comment>> weiBosAndComments = weiBoController.getWeiBosAndComments();
//
//        Set<Map.Entry<WeiBo, List<Comment>>> entries = weiBosAndComments.entrySet();// 遍历hashmap每个key value
////        Map.Entry<WeiBo, List<Comment>> 这是一个类
//        for (Map.Entry<WeiBo, List<Comment>> entry : entries) {
//            WeiBo weiBo = entry.getKey();
//            List<Comment> comments = entry.getValue();
//            System.out.println(weiBo);
//            for (Comment comment : comments) {
//                System.out.println(comment);
//            }
//        }
//     ----------------------------------------------------------------------------------------------------
        List<WeiBoVo> weiBosAndComments = weiBoController.getWeiBosAndComments();
        for (WeiBoVo weiBosAndComment : weiBosAndComments) {
            System.out.println(weiBosAndComment.getWeiBo());// 取出微博对象
            for (Comment comment : weiBosAndComment.getComments()) {
                System.out.println(comment);
            }
        }


    }

    public static void comment() {
        System.out.println("请输入要评论的ID");
        String id = scanner.next();
        weiBoController.comment("鸡你太美", id);
    }

    public static void getComment() {
        List<Comment> comments = weiBoController.getComments();
        for (Comment comment : comments) {
            System.out.println(comment);
        }
    }
}
